function formatNumber(n) {
    const str = n.toString()
    return str[1] ? str : `0${str}`
}

export function formatTime(date) {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()

    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    const t1 = [year, month, day].map(formatNumber).join('/')
    const t2 = [hour, minute, second].map(formatNumber).join(':')

    return `${t1} ${t2}`
}
// 微信异步api封装
export function asyncWrap(fn) {
    return (options = {}) => {
        return new Promise((resolve, reject) => {
            const conf = {
                success: res => {
                    resolve(res)
                },
                fail: err => {
                    reject(err)
                }
            }
            wx[fn](Object.assign(conf, options))
        })
    }
}
/* 获取当前页url*/
export function getCurrentPageUrl() {
    var pages = getCurrentPages() // 获取加载的页面
    if (Array.isArray(pages) && pages.length > 0) {
        var currentPage = pages[pages.length - 1] // 获取当前页面的对象
        var url = currentPage.route // 当前页面url
        return url
    } else {
        return ''
    }
}
// 获取当前页面的url参数
export function getCurrentPageParams() {
    var pages = getCurrentPages() // 获取加载的页面
    var currentPage = pages[pages.length - 1] // 获取当前页面的对象
    var options = currentPage && currentPage.options
    return options
    // return { house_id: 2 }
}

export default {
    formatNumber,
    formatTime,
    asyncWrap,
    getCurrentPageParams,
    getCurrentPageUrl
}
